<template>
  <div>
    <!-- 顶部导航 -->
    <div>
      <BackNav
        left-text="返回"
        left-arrow
        title="购物袋"
        right-text="编辑"
        @gopage="go"
      ></BackNav>
    </div>
    <!-- 商品信息 -->
    <div class="main">
      <!-- 商品列表 -->
      <div class="main-item" v-for="(item, index) in Cartlist" :key="index">
        <div class="left">
          <van-checkbox
            v-model="item.checked"
            @click="simplySelect"
            checked-color="#0c43ba"
          ></van-checkbox>
        </div>
        <div class="wrapper">
          <div class="img-box">
            <img :src="item.small_img" class="auto-img" alt="" />
          </div>
          <div class="right">
            <div class="top">
              <span class="top-left">{{ item.name }}</span>
              <span class="top-right">{{ item.rule }}</span>
            </div>
            <div class="enname">{{ item.enname }}</div>
            <div class="bottom">
              <div class="price">&yen;{{ item.price }}</div>
              <div class="step">
                <van-stepper
                  v-model="item.count"
                  theme="round"
                  button-size="20"
                  disable-input
                  @plus="modifyNum(item, 2)"
                  @minus="modifyNum(item, 1)"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 提交订单栏 -->
      <div class="order" v-show="Cartlist.length != 0" v-if="!isshow">
        <van-submit-bar
          :price="getPrice"
          button-text="提交订单"
          @submit="onSubmit"
          button-color="#0c43ba"
        >
          <van-checkbox
            v-model="show"
            @click="allselect"
            checked-color="#0c43ba"
            >全选</van-checkbox
          >
        </van-submit-bar>
      </div>
      <!-- 删除选择 -->
      <div class="order" v-if="isshow" v-show="Cartlist.length != 0">
        <van-submit-bar
          button-text="删除选择"
          @submit="onSubmit1"
          button-color="#EE0A24"
        >
          <van-checkbox
            checked-color="#0c43ba"
            v-model="show"
            @click="allselect"
            >全选</van-checkbox
          >
        </van-submit-bar>
      </div>
    </div>
  </div>
</template>
<script>
import BackNav from "../components/BackNav";
export default {
  name: "ShopBag",
  components: {
    BackNav,
  },
  data() {
    return {
      // 单选按钮
      show: false,
      Cartlist: [],
      sids: [],
      // 记录订单数据
      orderlist: [],
      afterArr: [],
      isshow: false,
    };
  },
  computed: {
    // 计算价格
    getPrice() {
      let total = 0;
      for (let i = 0; i < this.Cartlist.length; i++) {
        if (this.Cartlist[i].checked) {
          total += this.Cartlist[i].price * this.Cartlist[i].count * 100;
        }
      }
      return total;
    },
  },
  created() {
    this.getCartList();
  },
  methods: {
    // 控制删除和提交按钮的显示
    go() {
      this.isshow = !this.isshow;
    },
    // 提交订单
    onSubmit() {
      let a = [];
      for (let i = 0; i < this.Cartlist.length; i++) {
        if (this.Cartlist[i].checked == true) {
          a.push(this.Cartlist[i].sid);
        }
      }
      this.sids = JSON.stringify(a);
      localStorage.setItem("sids", this.sids);
      this.axios({
        method: "get",
        url: "/commitShopcart",
        params: {
          sids: this.sids,
        },
      })
        .then((res) => {
          if (res.data.code == 50000) {
            this.$toast(res.data.msg);
            res.data.result.forEach((item) => {
              this.orderlist.push(item);
            });
            let newArray = [];
            let a = [];
            for (let i = 0; i < this.orderlist.length; i++) {
              if (!a.includes(this.orderlist[i]["name"])) {
                newArray.push(this.orderlist[i]);
                a.push(this.orderlist[i]["name"]);
              } else {
                for (let j = 0; j < newArray.length; j++) {
                  if (newArray[j]["name"] == this.orderlist[i]["name"]) {
                    newArray[j].count += this.orderlist[i].count;
                    break;
                  }
                }
              }
            }
            this.afterArr = newArray;
            localStorage.setItem("id", JSON.stringify(this.afterArr));
            this.$router.push({
              name: "CheckOut",
            });
          }
        })
        .catch((err) => {
          
        });
    },
    // 全选方法
    allselect() {
      
      this.Cartlist.forEach((v) => {
        if (this.show) {
          v.checked = true;
        } else {
          v.checked = false;
        }
      });
    },
    // 获取购物车商品条目
    getCartList() {
      this.axios({
        method: "get",
        url: "/findAllShopcart",
      })
        .then((res) => {
          let a = [];
          if (res.data.code == 5000) {
            res.data.result.forEach((v) => {
              if (v.count != 0) {
                v.checked = false;
                a.push(v);
              }
            });
          }
          this.Cartlist = a;
        })
        .catch((err) => {
          
        });
    },
    // 单选方法
    simplySelect() {
      for (let i = 0; i < this.Cartlist.length; i++) {
        if (!this.Cartlist[i].checked) {
          this.show = false;
          return;
        }
        this.show = true;
      }
    },
    // 修改数量方法
    modifyNum(item, num) {
      let count = 0;
      if (num == 1) {
        count = item.count - 1;
      } else {
        count = item.count + 1;
      }
      this.axios({
        method: "post",
        url: "/modifyShopcartCount",
        data: {
          sid: item.sid,
          count,
        },
      })
        .then((res) => {
          
          if (res.data.code == 6000) {
            this.$toast(res.data.msg);
            
          }
        })
        .catch((err) => {
          
        });
    },
    // 删除选择
    onSubmit1() {
      let a = [];
      for (let i = 0; i < this.Cartlist.length; i++) {
        if (this.Cartlist[i].checked == true) {
          a.push(this.Cartlist[i].sid);
        }
      }
      this.sids = JSON.stringify(a);
      this.axios({
        method: "post",
        url: "/removeShopcart",
        data: {
          sids: this.sids,
        },
      })
        .then((res) => {
          // 
          if (res.data.code == 7000) {
            this.$toast(res.data.msg);
            setTimeout(() => {
              this.getCartList();
              this.show = false;
            }, 500);
          }
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>
<style lang="less" scoped>
.main {
  position: fixed;
  top: 46px;
  bottom: 50px;
  right: 0;
  left: 0;
  background: #f0f0f0;
  overflow: auto;
  padding: 0 10px;
  .main-item {
    background: #fff;
    margin-top: 10px;
    padding: 10px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    .wrapper {
      display: flex;
      .img-box {
        width: 80px;
        height: 80px;
        margin: 0 10px;
      }
      .right {
        .top {
          margin-bottom: 5px;
          font-size: 14px;
          .top-left {
            color: #666768;
          }
          .top-right {
            color: #ababab;
            margin-left: 10px;
          }
        }
        .enname {
          color: #b5b5b5;
          margin-bottom: 20px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          width: 200px;
        }
        .bottom {
          display: flex;
          align-items: center;
          .price {
            color: #0c34ba;
            font-weight: bold;
            font-size: 14px;
          }
          .step {
            margin-left: auto;
          }
        }
      }
    }
  }
}
/deep/ .van-checkbox {
  margin-right: auto;
}
/deep/ .van-stepper--round .van-stepper__plus {
  background-color: #0c34ba;
}
/deep/ .van-stepper--round .van-stepper__minus {
  border-color: #0c34ba;
  color: #0c34ba;
}
/deep/ .van-submit-bar {
  position: fixed;
  bottom: 50px;
}
/deep/ .van-submit-bar__price {
  color: #0c43ba;
  font-weight: 500;
}
</style>